{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "mnist = tf.keras.datasets.mnist\n",
    "(train_images,train_labels),(test_images,test_labels) = mnist.load_data()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_images = train_images / 255.0\n",
    "test_images = test_images / 255.0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_labels_ohe = tf.one_hot(train_labels,depth=10).numpy()\n",
    "test_labels_ohe = tf.one_hot(test_labels,depth = 10).numpy()\n",
    "\n",
    "myW = tf.Variable(np.zeros((784,64)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = tf.keras.models.Sequential()\n",
    "model.add(tf.keras.layers.Flatten(input_shape = (28,28)))\n",
    "model.add(tf.keras.layers.Dense(units = 64,activation = \"relu\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.Dense(units = 32,kernel_initializer = \"normal\",activation = \"relu\"))\n",
    "model.add(tf.keras.layers.Dense(units = 10,activation = \"softmax\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten_1 (Flatten)          (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 64)                50240     \n",
      "_________________________________________________________________\n",
      "dense_4 (Dense)              (None, 32)                2080      \n",
      "_________________________________________________________________\n",
      "dense_5 (Dense)              (None, 10)                330       \n",
      "=================================================================\n",
      "Total params: 52,650\n",
      "Trainable params: 52,650\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer=\"adam\",loss = \"categorical_crossentropy\",metrics=[\"accuracy\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_epochs = 10\n",
    "batch_size = 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "1600/1600 - 3s - loss: 0.3557 - accuracy: 0.8983 - val_loss: 0.1886 - val_accuracy: 0.9470\n",
      "Epoch 2/10\n",
      "1600/1600 - 3s - loss: 0.1552 - accuracy: 0.9538 - val_loss: 0.1260 - val_accuracy: 0.9637\n",
      "Epoch 3/10\n",
      "1600/1600 - 3s - loss: 0.1098 - accuracy: 0.9675 - val_loss: 0.1141 - val_accuracy: 0.9669\n",
      "Epoch 4/10\n",
      "1600/1600 - 3s - loss: 0.0856 - accuracy: 0.9736 - val_loss: 0.1050 - val_accuracy: 0.9698\n",
      "Epoch 5/10\n",
      "1600/1600 - 3s - loss: 0.0699 - accuracy: 0.9783 - val_loss: 0.1090 - val_accuracy: 0.9694\n",
      "Epoch 6/10\n",
      "1600/1600 - 3s - loss: 0.0584 - accuracy: 0.9820 - val_loss: 0.1061 - val_accuracy: 0.9713\n",
      "Epoch 7/10\n",
      "1600/1600 - 3s - loss: 0.0497 - accuracy: 0.9850 - val_loss: 0.1046 - val_accuracy: 0.9712\n",
      "Epoch 8/10\n",
      "1600/1600 - 3s - loss: 0.0429 - accuracy: 0.9866 - val_loss: 0.1080 - val_accuracy: 0.9703\n",
      "Epoch 9/10\n",
      "1600/1600 - 3s - loss: 0.0371 - accuracy: 0.9883 - val_loss: 0.1196 - val_accuracy: 0.9698\n",
      "Epoch 10/10\n",
      "1600/1600 - 3s - loss: 0.0341 - accuracy: 0.9886 - val_loss: 0.1016 - val_accuracy: 0.9732\n"
     ]
    }
   ],
   "source": [
    "train_history = model.fit(train_images,train_labels_ohe,validation_split=0.2,batch_size = batch_size,verbose = 2, epochs = train_epochs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60000, 28, 28)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.04940931, -0.07733789,  0.02668978, ..., -0.0032507 ,\n",
       "        -0.06316141,  0.02487117],\n",
       "       [-0.05352732,  0.02116816,  0.02454767, ...,  0.05568912,\n",
       "        -0.06114899,  0.01956887],\n",
       "       [ 0.0412099 ,  0.04460549,  0.08293023, ..., -0.02777171,\n",
       "        -0.01672693,  0.00135546],\n",
       "       ...,\n",
       "       [-0.07086513, -0.06373015,  0.02067082, ..., -0.06328818,\n",
       "         0.05633298, -0.00852928],\n",
       "       [ 0.00576862, -0.01809416, -0.02541877, ..., -0.0164838 ,\n",
       "         0.07409797,  0.04549573],\n",
       "       [-0.01799732, -0.0203153 , -0.01991706, ...,  0.0546032 ,\n",
       "        -0.03519834,  0.04641505]], dtype=float32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.trainable_variables[0].numpy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
